// pages/newDetail/index.js
const app = getApp();
import req from '../../utils/Request.js';
import appReq from '../../utils/AppRequest.js';
var WxParse = require('../../utils/wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: {},
    arr: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getNews(options.keyId);
    this.addNum(options.keyId);
  },


  getNews: function (keyId) {
    wx.showLoading({
      title: '加载中',
    })
    req.get('/housesNews/detail.do', {keyId}).then(res => {
      console.log(res)
      if (res.code == 1) {
        this.getList(keyId)
        this.setData({
          list: res.data,
        })
        console.log(this.data.list.newsContent)
        WxParse.wxParse('article', 'html', this.data.list.newsContent, this, 0);
      } else {
        wx.hideLoading();
        app.showErrorMessage(res, '加载失败');
        return;
      }
    }).catch(res => {
      wx.hideLoading();
      wx.showToast({
        title: '网络异常',
        icon: 'none'
      });
    });
  },

  getList: function (id) {
    req.get('/index/newsLimit.do', {
      keyId: id
    }).then(res => {
      console.log(res)
      if (res.code == 1) {
        this.setData({
          arr: res.data
        })
        wx.hideLoading();
      } else {
        app.showErrorMessage(res, '加载失败');
        wx.hideLoading();
        return;
      }
    }).catch(res => {
      wx.hideLoading();
      wx.showToast({
        title: '网络异常',
        icon: 'none'
      });
    });
  },

  goNewDetail: function(e) {
    console.log(this.data.arr)
    let _index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '../newDetail/index?keyId=' + this.data.arr[_index].keyId
    })
  },

  addNum(id) {
    req.postFormData('/index/addReadAmount.do', {
      keyId: id
    }).then(res => {
      console.log(res)
      if (res.code == 1) {

      } else {
        app.showErrorMessage(res, res.msg);
        return;
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})